import Logger from '../base/logger';
import { validateCcsClient,dealWithCcsSendMessageResponse } from './ccs_client';

/**
 * 发送指令
 * @param {CcsClient} ccsClient 
 * @param {string} cmdName 
 * @param {boolean} askForPermission 
 * @param {string|object} cmdData 
 * @param {string} to 
 * @param {function} onSuccess 
 * @param {function} onFailure 
 */
export function sendPeerCmd (ccsClient, cmdName, askForPermission, cmdData, to, onSuccess, onFailure) {
    let isValid = validateCcsClient(ccsClient);
    if (isValid != 'ok') {
        onFailure(isValid);
        return;
    }
    ccsClient.sendPeerCmd(cmdName, askForPermission, cmdData, to, (status, resp) => {
        if (status === 'ok') {
            onSuccess(resp);
        } else {
            Logger.error(`ivcs-js-sdk-sendPeerCmd ${cmdName},error:${resp},at:${new Date().toISOString()}`);
            dealWithCcsSendMessageResponse(status,resp,onFailure);
            // onFailure(new IvcsError(ivcsErrorCodeEnum.ERROR_CCS_SESSION_MESSAGE_FAILED, resp));
        }
    });
}


/**
 * 取消指令
 * @param {CcsClient} ccsClient 
 * @param {string} cmdId 
 * @param {string} toWhom 
 * @param {function} onSuccess 
 * @param {function} onFailure 
 * @param {string} reason 取消原因 normal || timeout
 * @returns 
 */
export function cancelPeerCmd (ccsClient, cmdName, cmdId, toWhom, onSuccess, onFailure, reason = 'normal') {
    let isValid = validateCcsClient(ccsClient);
    if (isValid != 'ok') {
        onFailure(isValid);
        return;
    }
    ccsClient.sendPeerCancel(cmdName, cmdId, toWhom, (status, resp) => {
        if (status === 'ok') {
            onSuccess(resp);
        } else {
            Logger.error(`ivcs-js-sdk-cancelPeerCmd cmdName:${cmdName},cmdId:${cmdId},error:${resp},at:${new Date().toISOString()}`);
            dealWithCcsSendMessageResponse(status,resp,onFailure);
            // onFailure(new IvcsError(ivcsErrorCodeEnum.ERROR_CCS_SESSION_MESSAGE_FAILED, resp));
        }
    }, reason);
}


/**
 * 接受到指令的对方响应数据
 * @param {CcsClient} ccsClient 
 * @param {string} cmdName 
 * @param {function} ack 
 * @returns 
 */
export function onPeerCmdResponse (ccsClient, cmdName, ack) {
    let isValid = validateCcsClient(ccsClient);
    if (isValid != 'ok') {
        Logger.error(`ivcs-js-sdk:onPeerCmdResponse:${cmdName} error', ${isValid}`);
        return;
    }
    ccsClient.onPeerResponse(cmdName, resp => {
        ack(resp);
    });
}


/**
 * 收到指令
 * @param {CcsClient} ccsClient 
 * @param {string} cmdName 
 * @param {function} ack 
 * @returns 
 */
export function onPeerCmd (ccsClient, cmdName, ack) {
    let isValid = validateCcsClient(ccsClient);
    if (isValid != 'ok') {
        Logger.error(`ivcs-js-sdk:onPeerCmd:${cmdName} error', ${isValid}`);
        return;
    }
    ccsClient.onPeerCmd(cmdName, resp => {
        ack(resp);
    });
}


/**
 * 收到取消指令
 * @param {CcsClient} ccsClient 
 * @param {string} cmdName 
 * @param {function} ack 
 * @returns 
 */
export function onCancelPeerCmd (ccsClient, cmdName, ack) {
    let isValid = validateCcsClient(ccsClient);
    if (isValid != 'ok') {
        Logger.error(`ivcs-js-sdk:onCancelPeerCmd:${cmdName} error', ${isValid}`);
        return;
    }
    ccsClient.onPeerCancel(cmdName, resp => {
        ack(resp);
    });
}

/**
 * 响应指令结果
 * @param {CcsClient} ccsClient 
 * @param {string} MsgIdOfCmd 
 * @param {string} status 
 * @param {string|object} responseData 
 * @param {string} toWhom 
 * @param {function} onSuccess 
 * @param {function} onFailure 
 * @returns 
 */
export function responsePeerCmd (ccsClient, cmdName, MsgIdOfCmd, status, responseData, toWhom, onSuccess, onFailure) {
    let isValid = validateCcsClient(ccsClient);
    if (isValid != 'ok') {
        Logger.error(`ivcs-js-sdk-responsePeerCmd:${cmdName} cmdId:${MsgIdOfCmd} error', ${isValid}`);
        return;
    }
    ccsClient.sendPeerResponse(cmdName, MsgIdOfCmd, status, responseData, toWhom, (status, resp) => {
        if (status === 'ok') {
            onSuccess(resp);
        } else {
            Logger.info(`ivcs-js-sdk-responsePeerCmd:${cmdName} cmdId:${MsgIdOfCmd} error,${resp},${new Date().toISOString()}`);
            // onFailure(new IvcsError(ivcsErrorCodeEnum.ERROR_CCS_SESSION_MESSAGE_FAILED, resp));
            dealWithCcsSendMessageResponse(status,resp,onFailure);
        }
    });
}


